<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="author" content="lijinbo" />
    <title>122-css星空背景</title>
    <style>
      .starry_sky_max {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        z-index: -1;
        overflow: hidden;
        box-sizing: border-box;
        background: radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%);
      }
      @keyframes animStar {
        from {
          transform: translateY(0px);
        }

        to {
          transform: translateY(-2000px);
        }
      }
    </style>
  </head>

  <body>
    <div id="container" class="starry_sky_max"></div>
    <script>
      // 生成星空背景
      const r = (e) => ~~(Math.random() * e)
      function createBoxShadow(count) {
        const list = []
        for (let i = 0; i < count; i++) {
          list.push(`${r(2000)}px ${r(2000)}px #fff`)
        }
        return list.toString()
      }
      function createStarsDom(size, count) {
        const div = document.createElement('div')
        div.style.width = size + 'px'
        div.style.height = size + 'px'
        div.style.background = 'transparent'
        div.style.boxShadow = createBoxShadow(count)
        div.style.animation = `animStar ${size * 50}s linear infinite`
        return div
      }
      const container = document.getElementById('container')
      container.append(createStarsDom(1, 500))
      container.append(createStarsDom(2, 300))
      container.append(createStarsDom(3, 100))
    </script>
  </body>
</html>
